clear all
set mem 9999999
set more off


cd "$dir\Raw_Data"

use base_with_religion.dta

capture drop _merge
merge m:1 school1 using "Schools12.dta"
drop _merge
merge m:1 school1 using languagetest.dta
replace language="Dual" if language=="dual"
replace language="" if language=="N/A"
tab language, gen(lang)
drop _merge
merge m:1 school1 using tuitiontest.dta
drop _merge




drop  islam_school christ_school stmusl  stcath christianchrist3 christianislam3   islamicchrist3 islamicislam3

 cd "$dir\Final_Data"
merge m:1 ID using Final_sample.dta, keepusing(islam_school christ_school stmusl  stcath christianchrist3 christianislam3   islamicchrist3 islamicislam3)

  

  

  
  *gen course indicator
  	gen course=Subject + CourseNum
		encode course, gen(course1)

	gen course_term=course1 +Term
egen group_co_term =group(course_term)

	gen course_year=course1 +year
egen group_co_year =group(course_year)

encode(Dept), gen(dpt)
encode(Rank), gen(Rank1)

global treat islam_school christ_school stmusl  stcath ///
 christianchrist3 christianislam3  ///
 islamicchrist3 islamicislam3
 


**************************************************TABLE 5*******************************
cd "$dir\Tables"	
*Regression A: Peer match on Instructor Religion

* Table 6 Column 1 islamic school student same religion
qui{
	reghdfe musl_teach $treat i.year satmat satverb legacy sex*    if year>=2012 & year<=2017 & FR_nonFR_status =="never enrolled as FR" & satmat!=. & satverb!=. & MAJR_CODE!="MJRL"  & bla>10 & bla<=30 ,   absorb (i.dpt##i.year fake_advisor_id)  vce(cluster advisorIDyear)
	reghdfe musl_teach $treat i.year satmat satverb legacy sex*  advsex*  if e(sample) ,   absorb (i.dpt##c.year Rank1 year)  vce(cluster advisorIDyear)
gen sampletag=1 if e(sample)	
}

reghdfe musl_teach $treat i.year satmat satverb legacy sex*  advsex*  if sampletag==1 ,   absorb (i.dpt##c.year Rank1 year)  vce(cluster advisorIDyear)

	eststo zab
	** PEERS EFFECTS for STUDEnTS FROM ISLAMIC Schools*** (Islam match)
	lincomest  -stmusl-islamicislam3 //All Islamic schools peers relative to all Secular schools peers7
	eststo row51
	estimates restore zab
	lincomest  -stmusl-islamicislam3+stcath+islamicchrist3 //All Muslim school peers relative to all Christian schools peers
	eststo row61
	estimates restore zab
* Table 6 Column 1 christian school student same religion
	reghdfe christ_teach $treat i.year satmat satverb  legacy sex*  advsex*  if  sampletag==1,   absorb (i.dpt##c.year Rank1 year)  vce(cluster advisorIDyear)
	eststo zab2
	** PEERS EFFECTS for STUDEnTS FROM CHRISTIAN Schools*** (Christian match)
	lincomest -stcath-christianchrist3  //All Christian schools peers relative to all Secular schools peers
	eststo row31		
	estimates restore zab2
	lincomest -stcath-christianchrist3+stmusl+christianislam3 //All Christian schools peers relative to all Islamic schools peers
	eststo row41		
	estimates restore zab2
	
* Table 6 Column 1 secular school student same religion
	reghdfe no_teach $treat i.year satmat satverb  legacy sex*  advsex*  if  sampletag==1,   absorb (i.dpt##c.year Rank1 year)  vce(cluster advisorIDyear)

	eststo zab3
	** PEERS EFFECTS for STUDEnTS FROM SECULAR Schools*** (Secular match)
	lincomest stcath //All Secular schools peers relative to all Christian schools peers
	eststo row11		
	estimates restore zab3
	lincomest stmusl  //All Secular schools peers relative to all Islamic schools peers
	eststo row21		
	estimates restore zab3
	



*Regression B: Peer match on Instructor Religion 

	* Table 6 Column 2 islamic school student same religion


	reghdfe musl_teach $treat i.year satmat satverb legacy sex*    if  sampletag==1,   absorb (i.dpt##i.year fake_advisor_id)  vce(cluster advisorIDyear)
eststo zab
	** PEERS EFFECTS for STUDEnTS FROM ISLAMIC Schools*** (Islam match)
	lincomest  -stmusl-islamicislam3 //All Islamic schools peers relative to all Secular schools peers7
	eststo row52
	estimates restore zab
	lincomest  -stmusl-islamicislam3+stcath+islamicchrist3 //All Muslim school peers relative to all Christian schools peers
	eststo row62
	estimates restore zab

		* Table 6 Column 2 christian school student same religion
	reghdfe christ_teach $treat i.year satmat satverb legacy sex*   if  sampletag==1 ,   absorb (i.dpt##i.year  fake_advisor_id)  vce(cluster advisorIDyear)

	eststo zab2
	** PEERS EFFECTS for STUDEnTS FROM CHRISTIAN Schools*** (Christian match)
	lincomest -stcath-christianchrist3  //All Christian schools peers relative to all Secular schools peers
	eststo row32		
	estimates restore zab2
	lincomest -stcath-christianchrist3+stmusl+christianislam3 //All Christian schools peers relative to all Islamic schools peers
	eststo row42		
	estimates restore zab2
	
	* Table 6 Column 2 secular school student same religion
	reghdfe no_teach $treat i.year satmat satverb legacy sex*    if sampletag==1,   absorb (i.dpt##i.year fake_advisor_id)  vce(cluster advisorIDyear)

	eststo zab3
	** PEERS EFFECTS for STUDEnTS FROM SECULAR Schools*** (Secular match)
	lincomest stcath //All Secular schools peers relative to all Christian schools peers
	eststo row12		
	estimates restore zab3
	lincomest stmusl  //All Secular schools peers relative to all Islamic schools peers
	eststo row22		
	estimates restore zab3
	

	*Regression C: Peer match on Instructor Religion 
cd "$dir\Final_Data"
capture drop _merge
merge m:1 ID using Controls-Merger.dta
cd "$dir\Tables"	

gen missing_lang=(prop_eng==.& prop_french==.)
replace christ_eng  = 0 if prop_eng==.
replace musl_eng  = 0 if prop_eng==.
replace christ_french  = 0 if prop_french==.
replace musl_french  = 0 if prop_french==.
replace prop_eng=0 if prop_eng==.
replace prop_french=0 if prop_french==.


reghdfe musl_teach $treat  satmat satverb legacy sex*  advsex*  peerqual christ_peerqual musl_peerqual meanpeersex christmeanpeersex muslmeanpeersex prop_french christ_french musl_french  prop_eng christ_eng musl_eng missing_lang meantuition  christ_tuit musl_tuit  if year>=2012 & year<=2017 & FR_nonFR_status =="never enrolled as FR" & satmat!=. & satverb!=. & MAJR_CODE!="MJRL"  & bla>10 & bla<=30 ,     absorb (i.dpt##i.year fake_advisor_id)  vce(cluster advisorIDyear)

gen sampletag_musl=1 if e(sample)

eststo zab
** PEERS EFFECTS for STUDEnTS FROM ISLAMIC Schools*** (Islam match)
lincomest  -stmusl-islamicislam3 //All Islamic schools peers relative to all Secular schools peers7
eststo row54
estimates restore zab
lincomest  -stmusl-islamicislam3+stcath+islamicchrist3 //All Muslim school peers relative to all Christian schools peers
eststo row64
estimates restore zab

***** For christian students**********
reghdfe christ_teach $treat  satmat satverb  legacy sex*  advsex*  peerqual christ_peerqual musl_peerqual meanpeersex christmeanpeersex muslmeanpeersex prop_french christ_french musl_french  prop_eng christ_eng musl_eng missing_lang meantuition  christ_tuit musl_tuit  if year>=2012 & year<=2017 & FR_nonFR_status =="never enrolled as FR" & satmat!=. & satverb!=. & MAJR_CODE!="MJRL"  & bla>10 & bla<=30 ,     absorb (i.dpt##i.year fake_advisor_id)  vce(cluster advisorIDyear)
gen sampletag_christ=1 if e(sample)

eststo zab2
** PEERS EFFECTS for STUDEnTS FROM CHRISTIAN Schools*** (Christian match)
lincomest -stcath-christianchrist3  //All Christian schools peers relative to all Secular schools peers
eststo row34		
estimates restore zab2
lincomest -stcath-christianchrist3+stmusl+christianislam3 //All Christian schools peers relative to all Islamic schools peers
eststo row44		
estimates restore zab2

***** For secular students**********
reghdfe no_teach $treat  satmat satverb legacy sex*  advsex*  peerqual christ_peerqual musl_peerqual meanpeersex christmeanpeersex muslmeanpeersex prop_french christ_french musl_french  prop_eng christ_eng musl_eng missing_lang meantuition  christ_tuit musl_tuit  if year>=2012 & year<=2017 & FR_nonFR_status =="never enrolled as FR" & satmat!=. & satverb!=. & MAJR_CODE!="MJRL"  & bla>10 & bla<=30 ,    absorb (i.dpt##i.year fake_advisor_id)  vce(cluster advisorIDyear)

gen sampletag_sec=1 if e(sample)

eststo zab3
** PEERS EFFECTS for STUDEnTS FROM SECULAR Schools*** (Secular match)
lincomest stcath //All Secular schools peers relative to all Christian schools peers
eststo row14		
estimates restore zab3
lincomest stmusl  //All Secular schools peers relative to all Islamic schools peers
eststo row24		
estimates restore zab3



reghdfe musl_teach $treat  satmat satverb legacy sex*  advsex*   if sampletag_musl==1,     absorb (i.dpt##i.year fake_advisor_id)  vce(cluster advisorIDyear)



eststo zab
** PEERS EFFECTS for STUDEnTS FROM ISLAMIC Schools*** (Islam match)
lincomest  -stmusl-islamicislam3 //All Islamic schools peers relative to all Secular schools peers7
eststo row55
estimates restore zab
lincomest  -stmusl-islamicislam3+stcath+islamicchrist3 //All Muslim school peers relative to all Christian schools peers
eststo row65
estimates restore zab

***** For christian students**********
reghdfe christ_teach $treat  satmat satverb  legacy sex*  advsex*   if sampletag_christ==1 ,     absorb (i.dpt##i.year fake_advisor_id)  vce(cluster advisorIDyear)

eststo zab2
** PEERS EFFECTS for STUDEnTS FROM CHRISTIAN Schools*** (Christian match)
lincomest -stcath-christianchrist3  //All Christian schools peers relative to all Secular schools peers
eststo row35		
estimates restore zab2
lincomest -stcath-christianchrist3+stmusl+christianislam3 //All Christian schools peers relative to all Islamic schools peers
eststo row45	
estimates restore zab2

***** For secular students**********
reghdfe no_teach $treat  satmat satverb legacy sex*  advsex*    if sampletag_sec==1,    absorb (i.dpt##i.year fake_advisor_id)  vce(cluster advisorIDyear)

gen sampletag_sec=1 if e(sample)

eststo zab3
** PEERS EFFECTS for STUDEnTS FROM SECULAR Schools*** (Secular match)
lincomest stcath //All Secular schools peers relative to all Christian schools peers
eststo row15
estimates restore zab3
lincomest stmusl  //All Secular schools peers relative to all Islamic schools peers
eststo row25	
estimates restore zab3



esttab row11 row12 row14 row15   using Table5.tex, label  nocon  b(%9.3f) se(%9.3f) starlevels(* 0.1 ** 0.05 *** 0.01) replace
foreach i in  2 3 4 5 6{
esttab row`i'1  row`i'2  row`i'4  row`i'5 using Table5.tex, label  nocon  b(%9.3f) se(%9.3f) starlevels(* 0.1 ** 0.05 *** 0.01) append
}
	
estimates clear
	
	


***Table B4 ROBUSTNESS ***Year 1 
*islamic school students
capture estimates drop row* zab*
reghdfe musl_teach $treat i.year satmat satverb legacy sex*      if year>=2012 & year<=2017 & FR_nonFR_status =="never enrolled as FR" & satmat!=. & satverb!=. & MAJR_CODE!="MJRL"  & bla<10 ,  absorb(i.dpt##i.year fake_advisor_id) cluster(advisorIDyear)

eststo zab
** PEERS EFFECTS for STUDEnTS FROM ISLAMIC Schools*** (Islam match)
lincomest  -stmusl-islamicislam3 //All Islamic schools peers relative to all Secular schools peers7
eststo row51
estimates restore zab
lincomest  -stmusl-islamicislam3+stcath+islamicchrist3 //All Muslim school peers relative to all Christian schools peers
eststo row61
estimates restore zab

reghdfe christ_teach $treat i.year satmat satverb legacy sex*     if year>=2012 & year<=2017 & FR_nonFR_status =="never enrolled as FR" & satmat!=. & satverb!=. & MAJR_CODE!="MJRL" & bla<10 ,  absorb(i.dpt##i.year fake_advisor_id)  cluster(advisorIDyear)

eststo zab2

** PEERS EFFECTS for STUDEnTS FROM CHRISTIAN Schools*** (Christian match)
lincomest -stcath-christianchrist3  //All Christian schools peers relative to all Secular schools peers
eststo row31		
estimates restore zab2
lincomest -stcath-christianchrist3+stmusl+christianislam3 //All Christian schools peers relative to all Islamic schools peers
eststo row41		
estimates restore zab2

reghdfe no_teach $treat i.year satmat satverb  legacy sex*   if year>=2012 & year<=2017 & FR_nonFR_status =="never enrolled as FR" & satmat!=. & satverb!=.  & MAJR_CODE!="MJRL" & bla<10 , absorb(i.dpt##i.year fake_advisor_id)  cluster(advisorIDyear)

eststo zab3
** PEERS EFFECTS for STUDEnTS FROM SECULAR Schools*** (Secular match)
lincomest stcath //All Secular schools peers relative to all Christian schools peers
eststo row11		
estimates restore zab3
lincomest stmusl  //All Secular schools peers relative to all Islamic schools peers
eststo row21		
estimates restore zab3

esttab row11 using TableB4.tex, label  nocon  b(%9.3f) se(%9.3f) starlevels(* 0.1 ** 0.05 *** 0.01) replace
foreach i in  2 3 4 5 6{
esttab row`i'* using TableB4.tex, label  nocon  b(%9.3f) se(%9.3f) starlevels(* 0.1 ** 0.05 *** 0.01) append
}







